package com.admin.utils.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.DateFormatUtils;


public class DateUtil {
	public static String YYYY = "yyyy";

	public static String YYYY_MM = "yyyy-MM";

	public static String YYYY_MM_DD = "yyyy-MM-dd";

	public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static String YYYYMMDD = "yyyyMMdd";

	public static String YYYYMM = "yyyyMM";

	public static String YYYYMMDD000000 = "yyyyMMdd000000";

	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	

	public static Date StringToDate(String time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
		try {
			return df.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static Date StringToDate(String time, String datePatterm) {
		SimpleDateFormat df = new SimpleDateFormat(datePatterm);
		try {
			return df.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static String nowDate() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		return dateFormat.format(calendar.getTime());
	}

	public static String nowDate(String format) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(calendar.getTime());
	}

	public static String afterHours(Integer hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR, hour);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		return dateFormat.format(calendar.getTime());
	}

	public static String change(String time) {
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date dateString = null;
		try {
			dateString = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return format.format(dateString);
	}

	/**
	 * 获取到下一个整小时
	 * 
	 * @return
	 */
	public static Date getNextHour() {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.HOUR, 1);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTime();
	}

	public static Long getDatePoorTime(Date endDate, Date nowDate) {
		if (nowDate == null) {
			nowDate = getNowDate();
		}
		return endDate.getTime() - nowDate.getTime();
	}

	/**
	 * 获取当前Date型日期
	 * 
	 * @return Date() 当前日期
	 */
	public static Date getNowDate() {
		return new Date();
	}

	public static final String parseDateToStr(final String format, final Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 日期路径 即年/月/日 如20180808
	 */
	public static final String nowTime() {
		Date now = new Date();
		return DateFormatUtils.format(now, YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 计算2个时间差，如果结束时间没有就选择当前时间
	 * 
	 * @param beginTime 开始时间
	 * @param endTime   结束时间
	 * @return
	 */
	public static final Long dayDark(String beginTime, String endTime, String timeParam) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timeParam);
		LocalDate from = LocalDate.parse(beginTime, formatter);

		if (endTime == null) {
			return LocalDate.now().toEpochDay() - from.toEpochDay();
		} else {
			LocalDate end = LocalDate.parse(endTime, formatter);
			return end.toEpochDay() - from.toEpochDay();
		}

	}

	/**
	 * 计算时间段的同比（比如开始时间为本周一，结束时间为本周三，间隔-7天，则获取到上周一到上周三的时间端）
	 * 
	 * @param beginDate
	 * @param endDate
	 * @param gapDays
	 * @return
	 */
	public static final List<String> dayChain(Date beginDate, Date endDate, Integer gapDays) {
		//
		LocalDate beginTime = beginDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		LocalDate endTime = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		long gapTimes = endTime.toEpochDay() - beginTime.toEpochDay();
		// 上个同时间段
		LocalDate lastBeginTime = beginTime.plusDays(gapDays);
		LocalDate lastEndTime = lastBeginTime.plusDays(gapTimes);
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDD + "000000");
		List<String> list = new ArrayList<>();
		list.add(lastBeginTime.format(formatter));
		list.add(lastEndTime.format(formatter));
		return list;
	}

	/**
	 * 获取到时间的本周一日期，如果为空则默认去当前时间
	 * 
	 * @param time
	 * @return
	 */
	public static final Date getMonday(Date time) {
		LocalDate beginTime = time == null ? LocalDate.now()
				: time.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		LocalDate monday = beginTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = monday.atStartOfDay().atZone(zone).toInstant();
		return Date.from(instant);
	}

	/**
	 * 获取到时间间隔的时间日期
	 * 
	 * @param date
	 * @param gapDays
	 * @return
	 */
	public static final Date getDayGaps(Date date, int gapDays) {
		LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		localDate = localDate.plusDays(gapDays);
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
		return Date.from(instant);
	}

	/**
	 * 获取到时间间隔的时间日期(整时间小时)
	 * 
	 * @param date
	 * @param gapHours
	 * @return
	 */
	public static final Date getHourGaps(Date date, int gapHours) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.HOUR, gapHours);
		return ca.getTime();
	}

	/**
	 * 获取到今天凌晨时间
	 *
	 * @return
	 */
	public static Date getLinShi(Date date) {
		if (date == null) {
			date = new Date();
		}
		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),
				ZoneId.systemDefault());
		LocalDateTime day = localDateTime.with(LocalTime.MIN);
		Date endTime = Date.from(day.atZone(ZoneId.systemDefault()).toInstant());
		return endTime;
	}

	public static String getTimeStr(String time, String datePatterm) {
		DateFormat format = new SimpleDateFormat(datePatterm);
		Date dateString = null;
		try {
			dateString = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return format.format(dateString);
	}

	/**
	 * 获取到2个月份间隔的月份，包括本身
	 * 
	 * @param date1
	 * @param date2
	 * @param datePatterm
	 * @return
	 */
	public static List<String> getJgMon(String startDateStr, String endDateStr, String datePatterm) {
		try {
			Date startDate = new SimpleDateFormat(datePatterm).parse(startDateStr);
			Date endDate = new SimpleDateFormat(datePatterm).parse(endDateStr);

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(startDate);
			// 获取开始年份和开始月份
			int startYear = calendar.get(Calendar.YEAR);
			int startMonth = calendar.get(Calendar.MONTH);
			// 获取结束年份和结束月份
			calendar.setTime(endDate);
			int endYear = calendar.get(Calendar.YEAR);
			int endMonth = calendar.get(Calendar.MONTH);
			//
			List<String> list = new ArrayList<String>();
			for (int i = startYear; i <= endYear; i++) {
				String date = "";
				if (startYear == endYear) {
					for (int j = startMonth; j <= endMonth; j++) {
						if (j < 9) {
							date = i + "0" + (j + 1);
						} else {
							date = i + "" + (j + 1);
						}
						list.add(date);
					}

				} else {
					if (i == startYear) {
						for (int j = startMonth; j < 12; j++) {
							if (j < 9) {
								date = i + "0" + (j + 1);
							} else {
								date = i + "" + (j + 1);
							}
							list.add(date);
						}
					} else if (i == endYear) {
						for (int j = 0; j <= endMonth; j++) {
							if (j < 9) {
								date = i + "0" + (j + 1);
							} else {
								date = i + "" + (j + 1);
							}
							list.add(date);
						}
					} else {
						for (int j = 0; j < 12; j++) {
							if (j < 9) {
								date = i + "0" + (j + 1);
							} else {
								date = i + "" + (j + 1);
							}
							list.add(date);
						}
					}

				}

			}

			List<String> resList = new ArrayList<>();
			for (String str : list) {
				resList.add(DateUtil.parseDateToStr(datePatterm, StringToDate(str, YYYYMM)));
			}
			return resList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	  public static List<String> getMiddleDate(String beginStr, String endStr,String datePatterm) {
		    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePatterm);
		    LocalDate begin = LocalDate.parse(beginStr, formatter);
		    LocalDate end = LocalDate.parse(endStr, formatter);
		    
		    
	        List<String> localDateList = new ArrayList<>();
	        long length = end.toEpochDay() - begin.toEpochDay();
	        for (long i = length; i >= 0; i--) {
	            localDateList.add(end.minusDays(i).format(formatter));
	        }
	        return localDateList;
	    }

	  /**
	   * 获取到下一个月的当前时间
	   * @param nowDate
	   * @param datePatterm
	   * @return
	   */
	  public static String getNextMonth(Date nowDate,String datePatterm) {
		  LocalDate date = nowDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		  date = date.minusMonths(-1);	 
		  return  date.format(DateTimeFormatter.ofPattern(datePatterm));
	  }
	  
	

}
